package com.dingwen.treasure.kettle.init;

import com.dingwen.treasure.kettle.config.KettleConfig;
import com.dingwen.treasure.kettle.constant.KettleConstant;
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
import org.pentaho.di.core.KettleEnvironment;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Properties;

/**
 * 　KettleInitializing: kettle 环境初始化
 * 　@author dingwen
 * 　@date 2022/8/20
 */
@Component
@Log4j2
@Order(-2)
@DependsOn("kettleConfig")
public class KettleInit implements InitializingBean {

    @Resource
    private KettleConfig kettleConfig;

    @SneakyThrows
    @Override
    public void afterPropertiesSet() {
        // 自定义环境初始化
        customEnvironmentInit();
        // kettle环境初始化
        KettleEnvironment.init();
    }


    /**
     * 自定义环境初始化
     */
    private void customEnvironmentInit() {
        Properties properties = System.getProperties();
        properties.put(KettleConstant.KETTLE_HOME, kettleConfig.getHomePath());
        System.setProperties(properties);
        log.info("环境初始化成功～");
        log.info("kettle家目录：{}", kettleConfig.getHomePath());
    }
}
